<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>创建 | 小盐菌</title>
    <meta name="description" content="包含前端常用知识、源码阅读笔记、各种奇淫技巧、日常提效工具等">
    <link rel="preload stylesheet" href="/vitepress_note_public/assets/style.0e0c5bf7.css" as="style">
    
    <script type="module" src="/vitepress_note_public/assets/app.96a98344.js"></script>
    <link rel="preload" href="/vitepress_note_public/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/vitepress_note_public/assets/chunks/framework.521289a5.js">
    <link rel="modulepreload" href="/vitepress_note_public/assets/chunks/theme.0e4abe87.js">
    <link rel="modulepreload" href="/vitepress_note_public/assets/daily-notes_数组.md.ca3d7d97.lean.js">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon" href="/favicon.ico">
    <link rel="mask-icon" href="/favicon.ico" color="#3eaf7c">
    <meta name="msapplication-TileImage" content="/favicon.ico">
    <meta name="msapplication-TileColor" content="#000000">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-9318537a><!--[--><!--]--><!--[--><span tabindex="-1" data-v-ed1ac132></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-ed1ac132> Skip to content </a><!--]--><!----><header class="VPNav" data-v-9318537a data-v-8f624d97><div class="VPNavBar" data-v-8f624d97 data-v-ff5c4dd3><div class="container" data-v-ff5c4dd3><div class="title" data-v-ff5c4dd3><div class="VPNavBarTitle" data-v-ff5c4dd3 data-v-3833bba8><a class="title" href="/vitepress_note_public/" data-v-3833bba8><!--[--><!--]--><!--[--><img class="VPImage logo" src="/vitepress_note_public/logo.png" alt data-v-27d32eb7><!--]--><!--[-->小盐菌<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-ff5c4dd3><div class="curtain" data-v-ff5c4dd3></div><div class="content-body" data-v-ff5c4dd3><!--[--><!--]--><div class="VPNavBarSearch search" data-v-ff5c4dd3><!----></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-ff5c4dd3 data-v-28080106><span id="main-nav-aria-label" class="visually-hidden" data-v-28080106>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/vitepress_note_public/" tabindex="0" data-v-28080106 data-v-7a34f57c><!--[--><span data-v-7a34f57c>首页</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/vitepress_note_public/nav/" tabindex="0" data-v-28080106 data-v-7a34f57c><!--[--><span data-v-7a34f57c>待开发</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://notes.fe-mm.com" target="_blank" rel="noreferrer" tabindex="0" data-v-28080106 data-v-7a34f57c><!--[--><span data-v-7a34f57c>待开发</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://netease-music.fe-mm.com" target="_blank" rel="noreferrer" tabindex="0" data-v-28080106 data-v-7a34f57c><!--[--><span data-v-7a34f57c>待开发</span><!--]--></a><!--]--><!--[--><a class="VPLink link vp-external-link-icon VPNavBarMenuLink" href="https://github.com/maomao1996/tampermonkey-scripts" target="_blank" rel="noreferrer" tabindex="0" data-v-28080106 data-v-7a34f57c><!--[--><span data-v-7a34f57c>待开发</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-ff5c4dd3 data-v-1962e285><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-1962e285 data-v-799bb6cc data-v-73f5360c><span class="check" data-v-73f5360c><span class="icon" data-v-73f5360c><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-799bb6cc><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-799bb6cc><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-ff5c4dd3 data-v-cf8658e3 data-v-630d9035><!--[--><a class="VPSocialLink no-icon" href="https://github.com/maomao1996/vitepress-nav-template" aria-label="github" target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="https://weixin.qq.com/" aria-label target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg t="1703841371820" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7872" width="200" height="200"><path d="M683.058 364.695c11 0 22 1.016 32.943 1.976C686.564 230.064 538.896 128 370.681 128c-188.104 0.66-342.237 127.793-342.237 289.226 0 93.068 51.379 169.827 136.725 229.256L130.72 748.43l119.796-59.368c42.918 8.395 77.37 16.79 119.742 16.79 11 0 21.46-0.48 31.914-1.442a259.168 259.168 0 0 1-10.455-71.358c0.485-148.002 128.744-268.297 291.403-268.297l-0.06-0.06z m-184.113-91.992c25.99 0 42.913 16.79 42.913 42.575 0 25.188-16.923 42.579-42.913 42.579-25.45 0-51.38-16.85-51.38-42.58 0-25.784 25.93-42.574 51.38-42.574z m-239.544 85.154c-25.384 0-51.374-16.85-51.374-42.58 0-25.784 25.99-42.574 51.374-42.574 25.45 0 42.918 16.79 42.918 42.575 0 25.188-16.924 42.579-42.918 42.579z m736.155 271.655c0-135.647-136.725-246.527-290.983-246.527-162.655 0-290.918 110.88-290.918 246.527 0 136.128 128.263 246.587 290.918 246.587 33.972 0 68.423-8.395 102.818-16.85l93.809 50.973-25.93-84.677c68.907-51.93 120.286-119.815 120.286-196.033z m-385.275-42.58c-16.923 0-34.452-16.79-34.452-34.179 0-16.79 17.529-34.18 34.452-34.18 25.99 0 42.918 16.85 42.918 34.18 0 17.39-16.928 34.18-42.918 34.18z m188.165 0c-16.984 0-33.972-16.79-33.972-34.179 0-16.79 16.927-34.18 33.972-34.18 25.93 0 42.913 16.85 42.913 34.18 0 17.39-16.983 34.18-42.913 34.18z" fill="#09BB07" p-id="7873"></path></svg></a><a class="VPSocialLink no-icon" href="https://gitee.com/" aria-label target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg t="1703841714246" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9766" width="200" height="200"><path d="M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z" fill="#C71D23" p-id="9767"></path></svg></a><a class="VPSocialLink no-icon" href="https://www.bilibili.com/" aria-label target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg t="1703842691600" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10969" width="200" height="200"><path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#FF6B9B" p-id="10970"></path><path d="M336.896 115.584l117.568 115.968h115.008l117.632-115.904a36.672 36.672 0 0 1 51.328 0 35.456 35.456 0 0 1 0 50.56L672 231.552h75.648c33.664 0 65.92 13.184 89.792 36.672 23.808 23.488 37.12 55.296 37.12 88.512v286.08c0 33.216-13.312 65.024-37.12 88.512a127.872 127.872 0 0 1-89.792 36.672H276.288c-33.664 0-65.92-13.184-89.792-36.672a124.288 124.288 0 0 1-37.12-88.512v-286.08c0-33.216 13.312-65.024 37.12-88.512a127.872 127.872 0 0 1 89.792-36.672h75.584L285.568 166.144a35.456 35.456 0 0 1-9.344-34.56 36.608 36.608 0 0 1 60.736-16z m410.88 187.52H276.224a54.144 54.144 0 0 0-54.144 48.512l-0.256 5.12v286.08c0 27.648 21.248 50.752 49.152 53.44l5.248 0.192h471.424a54.144 54.144 0 0 0 54.144-48.448l0.256-5.12v-286.08a54.016 54.016 0 0 0-54.4-53.76z m-380.8 107.264c19.968 0 36.224 16 36.224 35.776v71.552a36.032 36.032 0 0 1-36.288 35.712 36.032 36.032 0 0 1-36.224-35.712V446.08c0-19.776 16.192-35.776 36.224-35.776z m290.112 0c20.032 0 36.224 16 36.224 35.776v71.552a36.032 36.032 0 0 1-36.224 35.712 36.032 36.032 0 0 1-36.288-35.712V446.08c0-19.776 16.256-35.776 36.288-35.776z" fill="#FF7BA6" p-id="10971"></path><path d="M741.632 390.72c2.496 0.448 22.208-3.968 23.04-2.56 1.28 1.92 10.048 65.152 7.68 65.6-2.24 0.384-18.56 4.16-18.56 4.16-1.28-8.576-11.968-61.76-12.16-67.2m31.36-6.08l8.64 68.032c4.352-0.192 22.144-1.472 23.872-1.664-2.56-25.728-6.912-66.368-6.912-66.368a65.536 65.536 0 0 0-25.6 0m-16.896 94.464s38.72-9.856 52.736-5.056c7.104 25.728 20.096 170.24 21.376 176.064-8.832 1.088-37.76 3.584-40 4.224-1.92-10.88-34.112-168.32-34.112-175.232m143.36-86.72c2.368 0.64 22.848-2.112 23.04-0.64 0.448 5.056 2.56 65.792 0.256 65.92l-18.688 1.728c-0.384-8.32-4.992-61.376-4.544-67.008m31.616-1.664l1.664 67.2c4.352 0 21.952 1.024 23.872 0.832-0.448-25.728 0-66.368 0-66.368a115.712 115.712 0 0 0-25.6-1.664m-27.008 90.24s39.616-5.248 52.992 1.024c2.944 30.144 2.56 170.688 2.944 176.512-8.96 0-37.696 0.448-40 0.832-0.384-10.88-16.512-171.456-15.936-178.368M812.16 308.224c22.848 116.416 40.192 315.328 40.832 328.064 0 0 18.048 0.448 38.336 1.728-11.968-125.44-26.624-325.76-26.368-332.288-5.056-5.696-52.8 2.496-52.8 2.496m-50.24 275.328c-4.416-32.64-121.216-69.76-186.944-57.792 0 0-8.192-72-11.328-141.76a1447.04 1447.04 0 0 1 0.192-127.68c-4.352-3.2-51.712 19.456-77.248 28.864 0 0 30.592 129.408 52.736 397.824 0 0 35.392 3.776 96.128-7.936s132.544-48.384 126.464-91.52m-142.784 58.88l-10.24-73.984c2.496-1.28 65.536 22.208 72.192 26.432-1.024 4.608-61.952 47.488-61.952 47.488m-364.16-251.648c2.56 0.448 22.272-3.968 23.04-2.56 1.28 1.92 10.112 65.152 7.808 65.6-2.304 0.384-18.688 4.16-18.688 4.16-1.28-8.576-11.904-61.76-12.16-67.2m31.424-6.08l8.64 68.032c4.352-0.192 22.144-1.472 23.872-1.664-2.752-25.728-6.912-66.368-6.912-66.368a65.536 65.536 0 0 0-25.6 0M269.44 479.104s38.784-9.856 52.8-5.056c7.104 25.728 20.096 170.24 21.376 176.064-8.832 1.088-37.76 3.584-40 4.224-1.92-10.88-34.176-168.32-34.176-175.232M412.864 392.32c2.304 0.64 22.848-2.112 23.04-0.64 0.448 5.056 2.56 65.792 0.256 65.92l-18.688 1.728c-0.384-8.32-4.8-61.376-4.608-67.008m31.616-1.664l1.728 67.2c4.352 0 21.952 1.024 23.872 0.832-0.448-25.728 0-66.368 0-66.368a129.92 129.92 0 0 0-25.6-1.664m-27.008 90.24s39.616-5.248 52.992 1.024c2.944 30.144 2.56 170.688 2.944 176.512-8.96 0-37.696 0.448-40 0.832-0.192-10.88-16.512-171.456-15.936-178.368M325.568 308.224c22.848 116.416 40.192 315.328 40.832 328.064 0 0 18.048 0.448 38.336 1.728-11.968-125.44-26.624-325.76-26.368-332.544-5.056-5.44-52.8 2.752-52.8 2.752m-50.24 275.328c-4.416-32.64-121.216-69.76-186.944-57.792 0 0-8.192-72-11.328-141.76a1447.04 1447.04 0 0 1 0.192-127.68C72.832 253.376 25.6 275.776 0 285.184c0 0 30.592 129.408 52.736 397.824 0 0 35.392 3.776 96.128-7.936s132.48-48.384 126.464-91.52m-142.784 58.88l-10.24-73.984c2.496-1.28 65.536 22.208 72.192 26.432-1.024 4.608-61.952 47.488-61.952 47.488" fill="#FFFFFF" p-id="10972"></path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-ff5c4dd3 data-v-ba4558fe data-v-77509512><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-77509512><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-77509512><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-77509512><div class="VPMenu" data-v-77509512 data-v-8d8238a7><!----><!--[--><!--[--><!----><div class="group" data-v-ba4558fe><div class="item appearance" data-v-ba4558fe><p class="label" data-v-ba4558fe>外观</p><div class="appearance-action" data-v-ba4558fe><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-ba4558fe data-v-799bb6cc data-v-73f5360c><span class="check" data-v-73f5360c><span class="icon" data-v-73f5360c><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-799bb6cc><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-799bb6cc><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-ba4558fe><div class="item social-links" data-v-ba4558fe><div class="VPSocialLinks social-links-list" data-v-ba4558fe data-v-630d9035><!--[--><a class="VPSocialLink no-icon" href="https://github.com/maomao1996/vitepress-nav-template" aria-label="github" target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="https://weixin.qq.com/" aria-label target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg t="1703841371820" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7872" width="200" height="200"><path d="M683.058 364.695c11 0 22 1.016 32.943 1.976C686.564 230.064 538.896 128 370.681 128c-188.104 0.66-342.237 127.793-342.237 289.226 0 93.068 51.379 169.827 136.725 229.256L130.72 748.43l119.796-59.368c42.918 8.395 77.37 16.79 119.742 16.79 11 0 21.46-0.48 31.914-1.442a259.168 259.168 0 0 1-10.455-71.358c0.485-148.002 128.744-268.297 291.403-268.297l-0.06-0.06z m-184.113-91.992c25.99 0 42.913 16.79 42.913 42.575 0 25.188-16.923 42.579-42.913 42.579-25.45 0-51.38-16.85-51.38-42.58 0-25.784 25.93-42.574 51.38-42.574z m-239.544 85.154c-25.384 0-51.374-16.85-51.374-42.58 0-25.784 25.99-42.574 51.374-42.574 25.45 0 42.918 16.79 42.918 42.575 0 25.188-16.924 42.579-42.918 42.579z m736.155 271.655c0-135.647-136.725-246.527-290.983-246.527-162.655 0-290.918 110.88-290.918 246.527 0 136.128 128.263 246.587 290.918 246.587 33.972 0 68.423-8.395 102.818-16.85l93.809 50.973-25.93-84.677c68.907-51.93 120.286-119.815 120.286-196.033z m-385.275-42.58c-16.923 0-34.452-16.79-34.452-34.179 0-16.79 17.529-34.18 34.452-34.18 25.99 0 42.918 16.85 42.918 34.18 0 17.39-16.928 34.18-42.918 34.18z m188.165 0c-16.984 0-33.972-16.79-33.972-34.179 0-16.79 16.927-34.18 33.972-34.18 25.93 0 42.913 16.85 42.913 34.18 0 17.39-16.983 34.18-42.913 34.18z" fill="#09BB07" p-id="7873"></path></svg></a><a class="VPSocialLink no-icon" href="https://gitee.com/" aria-label target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg t="1703841714246" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9766" width="200" height="200"><path d="M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z" fill="#C71D23" p-id="9767"></path></svg></a><a class="VPSocialLink no-icon" href="https://www.bilibili.com/" aria-label target="_blank" rel="noopener" data-v-630d9035 data-v-516a33a1><svg t="1703842691600" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="10969" width="200" height="200"><path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#FF6B9B" p-id="10970"></path><path d="M336.896 115.584l117.568 115.968h115.008l117.632-115.904a36.672 36.672 0 0 1 51.328 0 35.456 35.456 0 0 1 0 50.56L672 231.552h75.648c33.664 0 65.92 13.184 89.792 36.672 23.808 23.488 37.12 55.296 37.12 88.512v286.08c0 33.216-13.312 65.024-37.12 88.512a127.872 127.872 0 0 1-89.792 36.672H276.288c-33.664 0-65.92-13.184-89.792-36.672a124.288 124.288 0 0 1-37.12-88.512v-286.08c0-33.216 13.312-65.024 37.12-88.512a127.872 127.872 0 0 1 89.792-36.672h75.584L285.568 166.144a35.456 35.456 0 0 1-9.344-34.56 36.608 36.608 0 0 1 60.736-16z m410.88 187.52H276.224a54.144 54.144 0 0 0-54.144 48.512l-0.256 5.12v286.08c0 27.648 21.248 50.752 49.152 53.44l5.248 0.192h471.424a54.144 54.144 0 0 0 54.144-48.448l0.256-5.12v-286.08a54.016 54.016 0 0 0-54.4-53.76z m-380.8 107.264c19.968 0 36.224 16 36.224 35.776v71.552a36.032 36.032 0 0 1-36.288 35.712 36.032 36.032 0 0 1-36.224-35.712V446.08c0-19.776 16.192-35.776 36.224-35.776z m290.112 0c20.032 0 36.224 16 36.224 35.776v71.552a36.032 36.032 0 0 1-36.224 35.712 36.032 36.032 0 0 1-36.288-35.712V446.08c0-19.776 16.256-35.776 36.288-35.776z" fill="#FF7BA6" p-id="10971"></path><path d="M741.632 390.72c2.496 0.448 22.208-3.968 23.04-2.56 1.28 1.92 10.048 65.152 7.68 65.6-2.24 0.384-18.56 4.16-18.56 4.16-1.28-8.576-11.968-61.76-12.16-67.2m31.36-6.08l8.64 68.032c4.352-0.192 22.144-1.472 23.872-1.664-2.56-25.728-6.912-66.368-6.912-66.368a65.536 65.536 0 0 0-25.6 0m-16.896 94.464s38.72-9.856 52.736-5.056c7.104 25.728 20.096 170.24 21.376 176.064-8.832 1.088-37.76 3.584-40 4.224-1.92-10.88-34.112-168.32-34.112-175.232m143.36-86.72c2.368 0.64 22.848-2.112 23.04-0.64 0.448 5.056 2.56 65.792 0.256 65.92l-18.688 1.728c-0.384-8.32-4.992-61.376-4.544-67.008m31.616-1.664l1.664 67.2c4.352 0 21.952 1.024 23.872 0.832-0.448-25.728 0-66.368 0-66.368a115.712 115.712 0 0 0-25.6-1.664m-27.008 90.24s39.616-5.248 52.992 1.024c2.944 30.144 2.56 170.688 2.944 176.512-8.96 0-37.696 0.448-40 0.832-0.384-10.88-16.512-171.456-15.936-178.368M812.16 308.224c22.848 116.416 40.192 315.328 40.832 328.064 0 0 18.048 0.448 38.336 1.728-11.968-125.44-26.624-325.76-26.368-332.288-5.056-5.696-52.8 2.496-52.8 2.496m-50.24 275.328c-4.416-32.64-121.216-69.76-186.944-57.792 0 0-8.192-72-11.328-141.76a1447.04 1447.04 0 0 1 0.192-127.68c-4.352-3.2-51.712 19.456-77.248 28.864 0 0 30.592 129.408 52.736 397.824 0 0 35.392 3.776 96.128-7.936s132.544-48.384 126.464-91.52m-142.784 58.88l-10.24-73.984c2.496-1.28 65.536 22.208 72.192 26.432-1.024 4.608-61.952 47.488-61.952 47.488m-364.16-251.648c2.56 0.448 22.272-3.968 23.04-2.56 1.28 1.92 10.112 65.152 7.808 65.6-2.304 0.384-18.688 4.16-18.688 4.16-1.28-8.576-11.904-61.76-12.16-67.2m31.424-6.08l8.64 68.032c4.352-0.192 22.144-1.472 23.872-1.664-2.752-25.728-6.912-66.368-6.912-66.368a65.536 65.536 0 0 0-25.6 0M269.44 479.104s38.784-9.856 52.8-5.056c7.104 25.728 20.096 170.24 21.376 176.064-8.832 1.088-37.76 3.584-40 4.224-1.92-10.88-34.176-168.32-34.176-175.232M412.864 392.32c2.304 0.64 22.848-2.112 23.04-0.64 0.448 5.056 2.56 65.792 0.256 65.92l-18.688 1.728c-0.384-8.32-4.8-61.376-4.608-67.008m31.616-1.664l1.728 67.2c4.352 0 21.952 1.024 23.872 0.832-0.448-25.728 0-66.368 0-66.368a129.92 129.92 0 0 0-25.6-1.664m-27.008 90.24s39.616-5.248 52.992 1.024c2.944 30.144 2.56 170.688 2.944 176.512-8.96 0-37.696 0.448-40 0.832-0.192-10.88-16.512-171.456-15.936-178.368M325.568 308.224c22.848 116.416 40.192 315.328 40.832 328.064 0 0 18.048 0.448 38.336 1.728-11.968-125.44-26.624-325.76-26.368-332.544-5.056-5.44-52.8 2.752-52.8 2.752m-50.24 275.328c-4.416-32.64-121.216-69.76-186.944-57.792 0 0-8.192-72-11.328-141.76a1447.04 1447.04 0 0 1 0.192-127.68C72.832 253.376 25.6 275.776 0 285.184c0 0 30.592 129.408 52.736 397.824 0 0 35.392 3.776 96.128-7.936s132.48-48.384 126.464-91.52m-142.784 58.88l-10.24-73.984c2.496-1.28 65.536 22.208 72.192 26.432-1.024 4.608-61.952 47.488-61.952 47.488" fill="#FFFFFF" p-id="10972"></path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-ff5c4dd3 data-v-0f5c76f7><span class="container" data-v-0f5c76f7><span class="top" data-v-0f5c76f7></span><span class="middle" data-v-0f5c76f7></span><span class="bottom" data-v-0f5c76f7></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav fixed reached-top" data-v-9318537a data-v-25546185><!----><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-25546185 data-v-11ffa2ae><button data-v-11ffa2ae>返回顶部</button><!----></div></div><!----><div class="VPContent" id="VPContent" data-v-9318537a data-v-4dd6dd46><div class="VPDoc has-aside" data-v-4dd6dd46 data-v-42620db0><!--[--><!--]--><div class="container" data-v-42620db0><div class="aside" data-v-42620db0><div class="aside-curtain" data-v-42620db0></div><div class="aside-container" data-v-42620db0><div class="aside-content" data-v-42620db0><div class="VPDocAside" data-v-42620db0 data-v-0770f702><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" role="navigation" data-v-0770f702 data-v-41840e04><div class="content" data-v-41840e04><div class="outline-marker" data-v-41840e04></div><div class="outline-title" role="heading" aria-level="2" data-v-41840e04>本页目录</div><nav aria-labelledby="doc-outline-aria-label" data-v-41840e04><span class="visually-hidden" id="doc-outline-aria-label" data-v-41840e04> Table of Contents for current page </span><ul class="root" data-v-41840e04 data-v-50a1839a><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-0770f702></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-42620db0><div class="content-container" data-v-42620db0><!--[--><!--]--><!----><main class="main" data-v-42620db0><div style="position:relative;" class="vp-doc _vitepress_note_public_daily-notes_%E6%95%B0%E7%BB%84" data-v-42620db0><div><h1 id="创建" tabindex="-1">创建 <a class="header-anchor" href="#创建" aria-label="Permalink to &quot;创建&quot;">​</a></h1><p><img src="/vitepress_note_public/assets/image-20220214103904752.2b67bcde.png" alt="image-20220214103904752"></p><h1 id="内置方法" tabindex="-1">内置方法 <a class="header-anchor" href="#内置方法" aria-label="Permalink to &quot;内置方法&quot;">​</a></h1><h2 id="es5" tabindex="-1">ES5 <a class="header-anchor" href="#es5" aria-label="Permalink to &quot;ES5&quot;">​</a></h2><h3 id="foreach" tabindex="-1">==forEach()== <a class="header-anchor" href="#foreach" aria-label="Permalink to &quot;==forEach()==&quot;">​</a></h3><ul><li><p>作用：<strong>简单的遍历数组。</strong>（即调用数组的每个元素，并将元素传递给回调函数。）</p></li><li><p>用法：</p><p><img src="/vitepress_note_public/assets/image-20220214114643235.124e64c0.png" alt="image-20220214114643235"></p></li><li><p><strong>返回值：</strong></p><ul><li><strong>无</strong></li><li><strong>不影响原数组</strong></li></ul></li></ul><h3 id="filter" tabindex="-1">==filter()== <a class="header-anchor" href="#filter" aria-label="Permalink to &quot;==filter()==&quot;">​</a></h3><ul><li><p>作用：</p><ul><li>filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。</li><li>主要用于<strong>筛选数组</strong>，找到所有满足的返回。</li></ul></li><li><p>用法：</p><div class="language-javascript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> arr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">56</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">15</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">48</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">7</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> newArr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> arr.</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">index</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">%</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">===</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">0</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newArr)</span></span>
<span class="line"><span style="color:#6A737D;">// [56, 48]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> arr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">56</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">15</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">48</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">7</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> newArr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> arr.</span><span style="color:#6F42C1;">filter</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">value</span><span style="color:#24292E;">, </span><span style="color:#E36209;">index</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">%</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">2</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">===</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">0</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newArr)</span></span>
<span class="line"><span style="color:#6A737D;">// [56, 48]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li><li><p><strong>返回值：返回值是一个新数组，不影响原数组。</strong></p></li></ul><h3 id="some" tabindex="-1">some() <a class="header-anchor" href="#some" aria-label="Permalink to &quot;some()&quot;">​</a></h3><ul><li><p>作用：用于检测数组中的元素是否满足指定条件。（<strong>找到一个满足的就返回</strong>）</p></li><li><p>用法：</p><div class="language-javascript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;red&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;pink&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;blue&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> flag1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> arr1.</span><span style="color:#B392F0;">some</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">==</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&#39;pink&#39;</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(flag1) </span><span style="color:#6A737D;">//true</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;red&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;pink&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;blue&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> flag1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> arr1.</span><span style="color:#6F42C1;">some</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">value</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">==</span><span style="color:#24292E;"> </span><span style="color:#032F62;">&#39;pink&#39;</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(flag1) </span><span style="color:#6A737D;">//true</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li><li><p><strong>返回值：</strong></p><ul><li><strong>返回一个布尔值。</strong></li><li>如果有一个元素满足条件，则表达式返回<em>true</em> , 剩余的元素不会再执行检测；如果没有满足条件的元素，则返回 false。</li><li><strong>不影响原数组。</strong></li></ul></li></ul><h3 id="map" tabindex="-1">==map()== <a class="header-anchor" href="#map" aria-label="Permalink to &quot;==map()==&quot;">​</a></h3><ul><li><p><strong>作用：</strong></p><ul><li><strong>遍历数组。</strong></li><li>返回一个新数组，数组中的元素为 原始数组元素调用函数处理后的值。（该方法接受一个回调函数作为参数，数组元素全部依次进入回调函数。）</li><li><strong>需要写<code>return</code> ，使用<code>Math.sqrt</code> 时不用写。</strong></li></ul></li><li><p>用法：</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">const</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">ary</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">9</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">16</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> result_1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> ary.</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">((</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">key</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">*</span><span style="color:#E1E4E8;"> value</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> result_2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> ary.</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Math.sqrt) </span><span style="color:#6A737D;">//Math.sqrt是平方根，把value依次传进去平方根然后返回</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(result_1) </span><span style="color:#6A737D;">// [16, 81, 256]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(result_2) </span><span style="color:#6A737D;">// [2, 3, 4]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">const</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">ary</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">9</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">16</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> result_1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> ary.</span><span style="color:#6F42C1;">map</span><span style="color:#24292E;">((</span><span style="color:#E36209;">value</span><span style="color:#24292E;">, </span><span style="color:#E36209;">key</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">*</span><span style="color:#24292E;"> value</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> result_2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> ary.</span><span style="color:#6F42C1;">map</span><span style="color:#24292E;">(Math.sqrt) </span><span style="color:#6A737D;">//Math.sqrt是平方根，把value依次传进去平方根然后返回</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(result_1) </span><span style="color:#6A737D;">// [16, 81, 256]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(result_2) </span><span style="color:#6A737D;">// [2, 3, 4]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li><li><p><strong>返回值：</strong></p><ul><li><strong>新数组</strong></li><li><strong>不影响原数组</strong></li></ul></li></ul><h3 id="every" tabindex="-1">every() <a class="header-anchor" href="#every" aria-label="Permalink to &quot;every()&quot;">​</a></h3><ol><li><p>作用：检测数组所有元素是否都符合指定条件（通过函数提供）。</p><ul><li>如果数组中检测到有一个元素不满足，则整个表达式返回 <em>false</em> ，且剩余的元素不会再进行检测。</li><li>如果所有元素都满足条件，则返回 true。</li></ul></li><li><p>返回值是布尔值</p></li><li><p>不影响原数组，不会对空数组检测</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> ages </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">23</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">40</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">30</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> result </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> ages.</span><span style="color:#B392F0;">every</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">index</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">&lt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">35</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(result) </span><span style="color:#6A737D;">// false</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> ages </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">5</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">23</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">18</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">40</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">30</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> result </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> ages.</span><span style="color:#6F42C1;">every</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">value</span><span style="color:#24292E;">, </span><span style="color:#E36209;">index</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">&lt;=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">35</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(result) </span><span style="color:#6A737D;">// false</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li></ol><h3 id="reverse" tabindex="-1">==reverse()== <a class="header-anchor" href="#reverse" aria-label="Permalink to &quot;==reverse()==&quot;">​</a></h3><ol><li><p><strong>作用：数组反转</strong></p></li><li><p><strong>返回值是反转之后的数组</strong></p></li><li><p><strong>影响原数组</strong></p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Mango&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">fruits.</span><span style="color:#B392F0;">reverse</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(fruits) </span><span style="color:#6A737D;">// [&#39;Mango&#39;, &#39;Apple&#39;, &#39;Orange&#39;, &#39;Banana&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Mango&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#24292E;">fruits.</span><span style="color:#6F42C1;">reverse</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(fruits) </span><span style="color:#6A737D;">// [&#39;Mango&#39;, &#39;Apple&#39;, &#39;Orange&#39;, &#39;Banana&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li></ol><h3 id="sort" tabindex="-1">==sort()== <a class="header-anchor" href="#sort" aria-label="Permalink to &quot;==sort()==&quot;">​</a></h3><ol><li><p><strong>作用：用于对数组进行排序。</strong></p></li><li><p><strong>数组元素可以是字母（字符串）或数字</strong>（默认排序顺序为按字母升序。）</p></li><li><p><strong>如果数组元素是字母（字符串），升序直接用，降序调用 <code>reverse</code> 方法反转即可</strong></p></li><li><p>**如果数组元素是数字，需要在 sort 方法中传入函数，**通过函数返回值控制升降序：如果该函数返回值小于 0，则不需要调换位置，大于 0 需要调换位置（可以简单记为：<strong>a-b 升序，b-a 降序</strong>）</p></li><li><p>语法：<code>array.sort(sortfunction)</code> ，只有数字数组排序时才需要传入函数</p></li><li><p><strong>返回值是排序之后的原数组</strong></p></li><li><p>**影响原数组 **</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#6A737D;">// 字母（字符串）数组的排序：</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Mango&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#6A737D;">// 字母升序（默认）</span></span>
<span class="line"><span style="color:#E1E4E8;">fruits.</span><span style="color:#B392F0;">sort</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(fruits) </span><span style="color:#6A737D;">// [&#39;Apple&#39;, &#39;Banana&#39;, &#39;Mango&#39;, &#39;Orange&#39;]</span></span>
<span class="line"><span style="color:#6A737D;">// reverse反转数组，形成了字母降序</span></span>
<span class="line"><span style="color:#E1E4E8;">fruits.</span><span style="color:#B392F0;">reverse</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(fruits) </span><span style="color:#6A737D;">// [&#39;Orange&#39;, &#39;Mango&#39;, &#39;Banana&#39;, &#39;Apple&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#6A737D;">// 字母（字符串）数组的排序：</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Mango&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#6A737D;">// 字母升序（默认）</span></span>
<span class="line"><span style="color:#24292E;">fruits.</span><span style="color:#6F42C1;">sort</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(fruits) </span><span style="color:#6A737D;">// [&#39;Apple&#39;, &#39;Banana&#39;, &#39;Mango&#39;, &#39;Orange&#39;]</span></span>
<span class="line"><span style="color:#6A737D;">// reverse反转数组，形成了字母降序</span></span>
<span class="line"><span style="color:#24292E;">fruits.</span><span style="color:#6F42C1;">reverse</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(fruits) </span><span style="color:#6A737D;">// [&#39;Orange&#39;, &#39;Mango&#39;, &#39;Banana&#39;, &#39;Apple&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#6A737D;">// 数字数组的排序：</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> points </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">40</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">100</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">25</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#6A737D;">// 数字升序</span></span>
<span class="line"><span style="color:#E1E4E8;">points.</span><span style="color:#B392F0;">sort</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">a</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">b</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">-</span><span style="color:#E1E4E8;"> b</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(points) </span><span style="color:#6A737D;">// [1, 5, 10, 25, 40, 100]</span></span>
<span class="line"><span style="color:#6A737D;">// 数字降序</span></span>
<span class="line"><span style="color:#E1E4E8;">points.</span><span style="color:#B392F0;">sort</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">a</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">b</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">-</span><span style="color:#E1E4E8;"> a</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(points) </span><span style="color:#6A737D;">// [100, 40, 25, 10, 5, 1]</span></span>
<span class="line"><span style="color:#6A737D;">// 即：a - b 小于0，不需要调换位置，大于0需要调换位置。</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#6A737D;">// 数字数组的排序：</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> points </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">40</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">100</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">5</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">25</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">10</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#6A737D;">// 数字升序</span></span>
<span class="line"><span style="color:#24292E;">points.</span><span style="color:#6F42C1;">sort</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">a</span><span style="color:#24292E;">, </span><span style="color:#E36209;">b</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">-</span><span style="color:#24292E;"> b</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(points) </span><span style="color:#6A737D;">// [1, 5, 10, 25, 40, 100]</span></span>
<span class="line"><span style="color:#6A737D;">// 数字降序</span></span>
<span class="line"><span style="color:#24292E;">points.</span><span style="color:#6F42C1;">sort</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">a</span><span style="color:#24292E;">, </span><span style="color:#E36209;">b</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">-</span><span style="color:#24292E;"> a</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(points) </span><span style="color:#6A737D;">// [100, 40, 25, 10, 5, 1]</span></span>
<span class="line"><span style="color:#6A737D;">// 即：a - b 小于0，不需要调换位置，大于0需要调换位置。</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li></ol><h3 id="splice" tabindex="-1">==splice()== <a class="header-anchor" href="#splice" aria-label="Permalink to &quot;==splice()==&quot;">​</a></h3><ul><li><p>作用：</p><ul><li><strong>用于添加或删除数组中的元素。</strong></li><li><strong>splice(第几个开始，要删的个数，要添加到数组的新元素)</strong></li></ul></li><li><p>用法：</p><div class="language-javascript vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">javascript</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#6A737D;">//-----添加用法------</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Mango&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">fruits.</span><span style="color:#B392F0;">splice</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">0</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Lemon&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Kiwi&#39;</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">//Banana,Orange,Lemon,Kiwi,Apple,Mango</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">//------删除用法-------</span></span>
<span class="line"><span style="color:#6A737D;">//从第三个位置开始删除数组后的两个元素：</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Mango&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">fruits.</span><span style="color:#B392F0;">splice</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">//Banana,Orange</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">//-----添加与删除----</span></span>
<span class="line"><span style="color:#6A737D;">//移除数组的第三个元素，并在数组第三个位置添加新元素:</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Mango&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">fruits.</span><span style="color:#B392F0;">splice</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Lemon&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Kiwi&#39;</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">//Banana,Orange,Lemon,Kiwi,Mango</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#6A737D;">//-----添加用法------</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Mango&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#24292E;">fruits.</span><span style="color:#6F42C1;">splice</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">0</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Lemon&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Kiwi&#39;</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">//Banana,Orange,Lemon,Kiwi,Apple,Mango</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">//------删除用法-------</span></span>
<span class="line"><span style="color:#6A737D;">//从第三个位置开始删除数组后的两个元素：</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Mango&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#24292E;">fruits.</span><span style="color:#6F42C1;">splice</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">//Banana,Orange</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">//-----添加与删除----</span></span>
<span class="line"><span style="color:#6A737D;">//移除数组的第三个元素，并在数组第三个位置添加新元素:</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Mango&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#24292E;">fruits.</span><span style="color:#6F42C1;">splice</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Lemon&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Kiwi&#39;</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">//Banana,Orange,Lemon,Kiwi,Mango</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li><li><p><strong>返回值：</strong></p><ul><li><p><strong>返回增/删之后的新数组</strong></p></li><li><p><strong>影响原数组</strong></p></li></ul></li></ul><h3 id="push" tabindex="-1">==push()== <a class="header-anchor" href="#push" aria-label="Permalink to &quot;==push()==&quot;">​</a></h3><ol><li><p><strong>作用：向数组的末尾添加一个或多个元素，并返回新的长度。</strong></p></li><li><p><strong>返回添加后的数组长度</strong></p></li><li><p><strong>影响原数组</strong></p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">push</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;Kiwi&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Lemon&#39;</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a) </span><span style="color:#6A737D;">// 4</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(fruits) </span><span style="color:#6A737D;">// [&#39;Banana&#39;, &#39;Orange&#39;, &#39;Kiwi&#39;, &#39;Lemon&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">push</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;Kiwi&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Lemon&#39;</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a) </span><span style="color:#6A737D;">// 4</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(fruits) </span><span style="color:#6A737D;">// [&#39;Banana&#39;, &#39;Orange&#39;, &#39;Kiwi&#39;, &#39;Lemon&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li></ol><h3 id="unshift" tabindex="-1">==unshift()== <a class="header-anchor" href="#unshift" aria-label="Permalink to &quot;==unshift()==&quot;">​</a></h3><ol><li><p><strong>作用：向数组的开头添加一个或更多元素，并返回新的长度。</strong></p></li><li><p><strong>返回添加后的数组长度</strong></p></li><li><p><strong>影响原数组</strong></p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">unshift</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;Kiwi&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Lemon&#39;</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a) </span><span style="color:#6A737D;">// 4</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(fruits) </span><span style="color:#6A737D;">// [&#39;Kiwi&#39;, &#39;Lemon&#39;, &#39;Banana&#39;, &#39;Orange&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">unshift</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;Kiwi&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Lemon&#39;</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a) </span><span style="color:#6A737D;">// 4</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(fruits) </span><span style="color:#6A737D;">// [&#39;Kiwi&#39;, &#39;Lemon&#39;, &#39;Banana&#39;, &#39;Orange&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li></ol><h3 id="pop" tabindex="-1">pop() <a class="header-anchor" href="#pop" aria-label="Permalink to &quot;pop()&quot;">​</a></h3><ol><li><p>作用：删除数组的最后一个元素并返回删除的元素</p></li><li><p>返回值是删除的元素</p></li><li><p>影响原数组</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">const</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">sites</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Google&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Runoob&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Taobao&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Zhihu&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Baidu&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(sites.</span><span style="color:#B392F0;">pop</span><span style="color:#E1E4E8;">())</span></span>
<span class="line"><span style="color:#6A737D;">// 输出结果为: &quot;Baidu&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(sites)</span></span>
<span class="line"><span style="color:#6A737D;">// 输出结果为: [&#39;Google&#39;, &#39;Runoob&#39;, &#39;Taobao&#39;, &#39;Zhihu&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">const</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">sites</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Google&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Runoob&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Taobao&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Zhihu&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Baidu&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(sites.</span><span style="color:#6F42C1;">pop</span><span style="color:#24292E;">())</span></span>
<span class="line"><span style="color:#6A737D;">// 输出结果为: &quot;Baidu&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(sites)</span></span>
<span class="line"><span style="color:#6A737D;">// 输出结果为: [&#39;Google&#39;, &#39;Runoob&#39;, &#39;Taobao&#39;, &#39;Zhihu&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li></ol><h3 id="shift" tabindex="-1">shift() <a class="header-anchor" href="#shift" aria-label="Permalink to &quot;shift()&quot;">​</a></h3><ol><li><p>作用：删除并返回数组的第一个元素</p></li><li><p>返回值是删除的元素</p></li><li><p>影响原数组</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">shift</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a) </span><span style="color:#6A737D;">// Banana</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(fruits) </span><span style="color:#6A737D;">// [&#39;Orange&#39;, &#39;Apple&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">shift</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a) </span><span style="color:#6A737D;">// Banana</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(fruits) </span><span style="color:#6A737D;">// [&#39;Orange&#39;, &#39;Apple&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li></ol><h3 id="slice" tabindex="-1">==slice()== <a class="header-anchor" href="#slice" aria-label="Permalink to &quot;==slice()==&quot;">​</a></h3><ol><li><p><strong>作用：区间抽取数组元素并返回。（字符串也是区间抽取）</strong></p></li><li><p><strong>包含开始位置元素 不包含结束位置元素</strong></p></li><li><p><strong>返回值是抽取的数组元素组成的数组</strong></p></li><li><p><strong>不影响原数组</strong></p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#6A737D;">// 语法：array.slice(start, end)</span></span>
<span class="line"><span style="color:#6A737D;">// 包含开始位置元素 不包含结束位置元素</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Lemon&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Mango&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">slice</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">slice</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">-</span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">-</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 截取倒数第三个（包含）到倒数第一个（不包含）的两个元素</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> c </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">slice</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">-</span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 截取最后三个元素</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a) </span><span style="color:#6A737D;">// [&#39;Orange&#39;, &#39;Lemon&#39;]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(b) </span><span style="color:#6A737D;">// [&#39;Lemon&#39;, &#39;Apple&#39;]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(c) </span><span style="color:#6A737D;">// [&#39;Lemon&#39;, &#39;Apple&#39;, &#39;Mango&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#6A737D;">// 语法：array.slice(start, end)</span></span>
<span class="line"><span style="color:#6A737D;">// 包含开始位置元素 不包含结束位置元素</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Lemon&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Mango&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">slice</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">slice</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">-</span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#D73A49;">-</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 截取倒数第三个（包含）到倒数第一个（不包含）的两个元素</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> c </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">slice</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">-</span><span style="color:#005CC5;">3</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 截取最后三个元素</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a) </span><span style="color:#6A737D;">// [&#39;Orange&#39;, &#39;Lemon&#39;]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(b) </span><span style="color:#6A737D;">// [&#39;Lemon&#39;, &#39;Apple&#39;]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(c) </span><span style="color:#6A737D;">// [&#39;Lemon&#39;, &#39;Apple&#39;, &#39;Mango&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li></ol><h3 id="reduce" tabindex="-1">==reduce()== <a class="header-anchor" href="#reduce" aria-label="Permalink to &quot;==reduce()==&quot;">​</a></h3><ol><li><p><strong>作用：常用作累加器</strong></p></li><li><p><strong>不影响原数组</strong></p></li><li><p><strong>返回值是数组元素累加的值</strong></p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> numbers </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">60</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">40</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> result </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> numbers.</span><span style="color:#B392F0;">reduce</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">total</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">num</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> total </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> num</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(result) </span><span style="color:#6A737D;">// 114</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> numbers </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">60</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">40</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">10</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">4</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> result </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> numbers.</span><span style="color:#6F42C1;">reduce</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">total</span><span style="color:#24292E;">, </span><span style="color:#E36209;">num</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> total </span><span style="color:#D73A49;">+</span><span style="color:#24292E;"> num</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(result) </span><span style="color:#6A737D;">// 114</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li></ol><h3 id="join" tabindex="-1">==join()== <a class="header-anchor" href="#join" aria-label="Permalink to &quot;==join()==&quot;">​</a></h3><ol><li><p><strong>作用：把数组转换成一个字符串，元素通过指定的分隔符进行分隔的。</strong></p></li><li><p><strong>返回值是一个字符串</strong></p></li><li><p><strong>不影响原数组</strong></p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> fruits </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;Banana&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Orange&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;Apple&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 不指定分隔符,默认逗号</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">join</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a) </span><span style="color:#6A737D;">// Banana,Orange,Apple</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">typeof</span><span style="color:#E1E4E8;"> a) </span><span style="color:#6A737D;">// string</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 指定分隔符</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> fruits.</span><span style="color:#B392F0;">join</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39; and &#39;</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(b) </span><span style="color:#6A737D;">// Banana and Orange and Apple</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> fruits </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;Banana&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Orange&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;Apple&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 不指定分隔符,默认逗号</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">join</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a) </span><span style="color:#6A737D;">// Banana,Orange,Apple</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">typeof</span><span style="color:#24292E;"> a) </span><span style="color:#6A737D;">// string</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 指定分隔符</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> fruits.</span><span style="color:#6F42C1;">join</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39; and &#39;</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(b) </span><span style="color:#6A737D;">// Banana and Orange and Apple</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li></ol><h2 id="es6" tabindex="-1">ES6 <a class="header-anchor" href="#es6" aria-label="Permalink to &quot;ES6&quot;">​</a></h2><h3 id="扩展运算符" tabindex="-1">扩展运算符 <a class="header-anchor" href="#扩展运算符" aria-label="Permalink to &quot;扩展运算符&quot;">​</a></h3><ol><li><p>作用：可以将数组或对象转成逗号分隔的序列</p></li><li><p>常用于合并数组</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> arr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">],</span></span>
<span class="line"><span style="color:#E1E4E8;">  arr1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">arr]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(arr1) </span><span style="color:#6A737D;">// [1, 2]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 数组含空位</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> arr2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, , </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">],</span></span>
<span class="line"><span style="color:#E1E4E8;">  arr3 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">arr2]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(arr3)</span></span>
<span class="line"><span style="color:#E1E4E8;">;[</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">undefined</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 合并数组</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">([</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">[</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">], </span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">[</span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">]]) </span><span style="color:#6A737D;">// [1, 2, 3, 4]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> arr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">],</span></span>
<span class="line"><span style="color:#24292E;">  arr1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">arr]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(arr1) </span><span style="color:#6A737D;">// [1, 2]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 数组含空位</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> arr2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, , </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">],</span></span>
<span class="line"><span style="color:#24292E;">  arr3 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">arr2]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(arr3)</span></span>
<span class="line"><span style="color:#24292E;">;[</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">undefined</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 合并数组</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">([</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">[</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">], </span><span style="color:#D73A49;">...</span><span style="color:#24292E;">[</span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">4</span><span style="color:#24292E;">]]) </span><span style="color:#6A737D;">// [1, 2, 3, 4]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li></ol><h3 id="array-from" tabindex="-1">Array.from() <a class="header-anchor" href="#array-from" aria-label="Permalink to &quot;Array.from()&quot;">​</a></h3><ol><li><p><strong>将拥有 <code>length</code> 属性的对象转化成数组</strong></p></li><li><p>返回值是数组</p></li><li><p>常用于将伪数组转化为数组，也可以将 Set 对象转化成数组</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#6A737D;">// Array.from() 第一个参数是接收要转化的对象</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Array.</span><span style="color:#B392F0;">from</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;RUNOOB&#39;</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 字符串本身含有length</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a) </span><span style="color:#6A737D;">// [&#39;R&#39;, &#39;U&#39;, &#39;N&#39;, &#39;O&#39;, &#39;O&#39;, &#39;B&#39;]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> obj </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#79B8FF;">0</span><span style="color:#E1E4E8;">: </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#6A737D;">//&#39;0&#39;:1中的&#39;0&#39;将转换为下标0,下面的key同理</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">: </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">,</span></span>
<span class="line"><span style="color:#E1E4E8;">  length: </span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">,</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> newObj </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Array.</span><span style="color:#B392F0;">from</span><span style="color:#E1E4E8;">(obj)</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newObj) </span><span style="color:#6A737D;">// [1, undefined, 3, undefined]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">/* Array.from() 第二个参数可选,是接收一个方法,</span></span>
<span class="line"><span style="color:#6A737D;">  对生成数组每个元素进行处理,然后将处理后的值返回数组*/</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> c </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#79B8FF;">0</span><span style="color:#E1E4E8;">: </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">,</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">: </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">,</span></span>
<span class="line"><span style="color:#E1E4E8;">  length: </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">,</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> r </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Array.</span><span style="color:#B392F0;">from</span><span style="color:#E1E4E8;">(c, (</span><span style="color:#FFAB70;">item</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> item </span><span style="color:#F97583;">*</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#6A737D;">// 或者写成如下:</span></span>
<span class="line"><span style="color:#6A737D;">// let r = Array.from(c, function (item) {</span></span>
<span class="line"><span style="color:#6A737D;">//   return item * 2</span></span>
<span class="line"><span style="color:#6A737D;">// })</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(r) </span><span style="color:#6A737D;">// [2, 4]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#6A737D;">// Array.from() 第一个参数是接收要转化的对象</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> Array.</span><span style="color:#6F42C1;">from</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;RUNOOB&#39;</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 字符串本身含有length</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a) </span><span style="color:#6A737D;">// [&#39;R&#39;, &#39;U&#39;, &#39;N&#39;, &#39;O&#39;, &#39;O&#39;, &#39;B&#39;]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> obj </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#005CC5;">0</span><span style="color:#24292E;">: </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#6A737D;">//&#39;0&#39;:1中的&#39;0&#39;将转换为下标0,下面的key同理</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">: </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">,</span></span>
<span class="line"><span style="color:#24292E;">  length: </span><span style="color:#005CC5;">4</span><span style="color:#24292E;">,</span></span>
<span class="line"><span style="color:#24292E;">}</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> newObj </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> Array.</span><span style="color:#6F42C1;">from</span><span style="color:#24292E;">(obj)</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newObj) </span><span style="color:#6A737D;">// [1, undefined, 3, undefined]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">/* Array.from() 第二个参数可选,是接收一个方法,</span></span>
<span class="line"><span style="color:#6A737D;">  对生成数组每个元素进行处理,然后将处理后的值返回数组*/</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> c </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#005CC5;">0</span><span style="color:#24292E;">: </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">,</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">: </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">,</span></span>
<span class="line"><span style="color:#24292E;">  length: </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">,</span></span>
<span class="line"><span style="color:#24292E;">}</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> r </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> Array.</span><span style="color:#6F42C1;">from</span><span style="color:#24292E;">(c, (</span><span style="color:#E36209;">item</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> item </span><span style="color:#D73A49;">*</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#6A737D;">// 或者写成如下:</span></span>
<span class="line"><span style="color:#6A737D;">// let r = Array.from(c, function (item) {</span></span>
<span class="line"><span style="color:#6A737D;">//   return item * 2</span></span>
<span class="line"><span style="color:#6A737D;">// })</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(r) </span><span style="color:#6A737D;">// [2, 4]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div></li></ol><h3 id="find" tabindex="-1">==find()== <a class="header-anchor" href="#find" aria-label="Permalink to &quot;==find()==&quot;">​</a></h3><ol><li><p><strong>作用：找出并返回第一个符合条件的数组成员，没找到就返回 undefined</strong></p></li><li><p><strong>返回值是 一个数组成员/undefined</strong></p></li><li><p><strong>不影响原数组</strong></p></li><li><p>找到了就不继续往后找，空数组不会执行</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> ages </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> result </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> ages.</span><span style="color:#B392F0;">find</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">index</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">&gt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">18</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(result) </span><span style="color:#6A737D;">// 18</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> ages </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">10</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">18</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">20</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> result </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> ages.</span><span style="color:#6F42C1;">find</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">value</span><span style="color:#24292E;">, </span><span style="color:#E36209;">index</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">&gt;=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">18</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(result) </span><span style="color:#6A737D;">// 18</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li></ol><h3 id="findindex" tabindex="-1">==findIndex()== <a class="header-anchor" href="#findindex" aria-label="Permalink to &quot;==findIndex()==&quot;">​</a></h3><ol><li><p>作用：找出并返回第一个符合条件的数组成员<strong>位置，没找到就返回 -1</strong></p></li><li><p><strong>返回值是 一个数组成员位置 / -1</strong></p></li><li><p><strong>不影响原数组</strong></p></li><li><p>找到了就不继续往后找，空数组不会执行</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> ages </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">10</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">18</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">20</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> result </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> ages.</span><span style="color:#B392F0;">findIndex</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">function</span><span style="color:#E1E4E8;"> (</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">, </span><span style="color:#FFAB70;">index</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  </span><span style="color:#F97583;">return</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">&gt;=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">18</span></span>
<span class="line"><span style="color:#E1E4E8;">})</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(result) </span><span style="color:#6A737D;">// 2</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> ages </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">10</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">18</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">20</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> result </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> ages.</span><span style="color:#6F42C1;">findIndex</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">function</span><span style="color:#24292E;"> (</span><span style="color:#E36209;">value</span><span style="color:#24292E;">, </span><span style="color:#E36209;">index</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  </span><span style="color:#D73A49;">return</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">&gt;=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">18</span></span>
<span class="line"><span style="color:#24292E;">})</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(result) </span><span style="color:#6A737D;">// 2</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li></ol><h3 id="includes" tabindex="-1">==includes()== <a class="header-anchor" href="#includes" aria-label="Permalink to &quot;==includes()==&quot;">​</a></h3><ol><li><p><strong>作用：查找数组中是否包含某个元素。</strong></p></li><li><p><strong>包含就返回 true，不包含就返回 false</strong></p></li><li><p><strong>返回值是布尔值</strong></p></li><li><p><strong>不影响原数组</strong></p></li><li><p>比 <code>some()</code> 方法更简洁</p></li><li><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;google&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;taobao&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> r1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> a.</span><span style="color:#B392F0;">includes</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> r2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> a.</span><span style="color:#B392F0;">includes</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;hello&#39;</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> r3 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> a.</span><span style="color:#B392F0;">includes</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;taobao&#39;</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> r4 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> a.</span><span style="color:#B392F0;">includes</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">NaN</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(r1, r2, r3, r4) </span><span style="color:#6A737D;">// true false true false</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;google&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;taobao&#39;</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> r1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> a.</span><span style="color:#6F42C1;">includes</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> r2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> a.</span><span style="color:#6F42C1;">includes</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;hello&#39;</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> r3 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> a.</span><span style="color:#6F42C1;">includes</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;taobao&#39;</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> r4 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> a.</span><span style="color:#6F42C1;">includes</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">NaN</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(r1, r2, r3, r4) </span><span style="color:#6A737D;">// true false true false</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li></ol><h2 id="小结" tabindex="-1">小结 <a class="header-anchor" href="#小结" aria-label="Permalink to &quot;小结&quot;">​</a></h2><ol><li><p>能遍历（迭代）数组的方法有：forEach()、map()、filter()、some()、every()。</p></li><li><p>数组方法对比表：</p><table><thead><tr><th>方法</th><th>返回值</th><th>影响原数组</th><th>描述</th></tr></thead><tbody><tr><td>==forEach==</td><td>无</td><td>否</td><td>遍历数组</td></tr><tr><td>==filter==</td><td>筛选后的数组</td><td>否</td><td>筛选数组</td></tr><tr><td>==map==</td><td>新数组</td><td>否</td><td>遍历数组</td></tr><tr><td>some</td><td>布尔值</td><td>否</td><td>遇到一个满足条件的数组元素就返回 true</td></tr><tr><td>every</td><td>布尔值</td><td>否</td><td>遇到一个不满足的数组元素就返回 false</td></tr><tr><td>==splice==</td><td>增/删元素之后的数组</td><td>是</td><td>指定位置增/删数组元素</td></tr><tr><td>==sort==</td><td>排序之后的数组</td><td>是</td><td>数组排序</td></tr><tr><td>==reverse==</td><td>反转之后的数组</td><td>是</td><td>数组反转</td></tr><tr><td>==push==</td><td>添加后的数组长度</td><td>是</td><td>数组末尾追加元素</td></tr><tr><td>==unshift==</td><td>添加后的数组长度</td><td>是</td><td>数组前面追加元素</td></tr><tr><td>pop</td><td>删除的元素</td><td>是</td><td>删除并返回数组最后一个元素</td></tr><tr><td>shift</td><td>删除的元素</td><td>是</td><td>删除并返回数组第一个元素</td></tr><tr><td>==slice==</td><td>抽取元素组成的数组</td><td>否</td><td>区间抽取数组元素</td></tr><tr><td>==reduce==</td><td>数组元素累加值</td><td>否</td><td>累加器</td></tr><tr><td>==join==</td><td>一个字符串</td><td>否</td><td>数组转成一个字符串</td></tr><tr><td>扩展运算符</td><td>逗号分隔的序列</td><td>否</td><td>拆分数组</td></tr><tr><td>Array.from</td><td>数组</td><td>/</td><td>将拥有 <code>length</code> 属性的对象转化成数组</td></tr><tr><td>==find==</td><td>一个数组成员/==undefined==</td><td>否</td><td>查找并返回第一个符合条件的数组成员</td></tr><tr><td>==findIndex==</td><td>一个数组成员位置 / ==-1==</td><td>否</td><td>查找并返回第一个符合条件的数组成员位置</td></tr><tr><td>==includes==</td><td>布尔值</td><td>否</td><td>判断某元素是否属于数组</td></tr></tbody></table></li><li><p>更多数组方法见<a href="https://www.runoob.com/jsref/jsref-obj-array.html" target="_blank" rel="noreferrer">https://www.runoob.com/jsref/jsref-obj-array.html</a></p></li></ol><h1 id="set-对象" tabindex="-1">Set 对象 <a class="header-anchor" href="#set-对象" aria-label="Permalink to &quot;Set 对象&quot;">​</a></h1><ol><li><p>Set 对象 类似于数组，但成员的值都是唯一的</p></li><li><p><strong>创建 Set 实例对象：</strong></p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> s </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#6A737D;">// 或者传入参数</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> s1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">])</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> s </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#6A737D;">// 或者传入参数</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> s1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">])</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li><li><p><strong>实例方法：</strong></p><p><code>add(value)</code> ：<strong>添加某个值，返回 Set 结构本身</strong></p><p><code>delete(value)</code> ：<strong>删除某个值，返回一个布尔值，表示删除是否成功</strong></p><p><code>has(value)</code> ：<strong>返回一个布尔值，表示该值是否为 Set 的成员</strong></p><p><code>clear()</code> ：<strong>清除所有成员，没有返回值</strong></p><p><code>size</code> ：<strong>返回实例的长度</strong></p><p><code>forEach()</code> ：<strong>和数组一样的 forEach</strong></p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> s </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">s.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">).</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 向Set实例中添加值</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> r1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> s.size </span><span style="color:#6A737D;">// 返回实例的长度</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(r1) </span><span style="color:#6A737D;">// 3</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> r2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> s.</span><span style="color:#B392F0;">delete</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 删除实例中的2值</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(r2) </span><span style="color:#6A737D;">// true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">s.</span><span style="color:#B392F0;">has</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 查看实例中是否有1这个值,返回布尔值</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">s.</span><span style="color:#B392F0;">clear</span><span style="color:#E1E4E8;">() </span><span style="color:#6A737D;">// 清除实例中所有的值</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">s.</span><span style="color:#B392F0;">forEach</span><span style="color:#E1E4E8;">((</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(value))</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> s </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">s.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">).</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">).</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">3</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 向Set实例中添加值</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> r1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> s.size </span><span style="color:#6A737D;">// 返回实例的长度</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(r1) </span><span style="color:#6A737D;">// 3</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> r2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> s.</span><span style="color:#6F42C1;">delete</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 删除实例中的2值</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(r2) </span><span style="color:#6A737D;">// true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">s.</span><span style="color:#6F42C1;">has</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 查看实例中是否有1这个值,返回布尔值</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">s.</span><span style="color:#6F42C1;">clear</span><span style="color:#24292E;">() </span><span style="color:#6A737D;">// 清除实例中所有的值</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">s.</span><span style="color:#6F42C1;">forEach</span><span style="color:#24292E;">((</span><span style="color:#E36209;">value</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(value))</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li><li><p>示例：</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> mySet </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#E1E4E8;">mySet.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// Set(1) {1}</span></span>
<span class="line"><span style="color:#E1E4E8;">mySet.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// Set(2) {1, 5}</span></span>
<span class="line"><span style="color:#E1E4E8;">mySet.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// Set(2) {1, 5} 这里体现了值的唯一性</span></span>
<span class="line"><span style="color:#E1E4E8;">mySet.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;some text&#39;</span><span style="color:#E1E4E8;">)</span></span>
<span class="line"><span style="color:#6A737D;">// Set(3) {1, 5, &quot;some text&quot;} 这里体现了类型的多样性</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> o </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> { a: </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, b: </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;"> }</span></span>
<span class="line"><span style="color:#E1E4E8;">mySet.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">(o)</span></span>
<span class="line"><span style="color:#E1E4E8;">mySet.</span><span style="color:#B392F0;">add</span><span style="color:#E1E4E8;">({ a: </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, b: </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;"> })</span></span>
<span class="line"><span style="color:#6A737D;">// Set(5) {1, 5, &quot;some text&quot;, {…}, {…}}</span></span>
<span class="line"><span style="color:#6A737D;">// 这里体现了对象之间引用不同不恒等，即使值相同，Set 也能存储</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> mySet </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#24292E;">mySet.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// Set(1) {1}</span></span>
<span class="line"><span style="color:#24292E;">mySet.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">5</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// Set(2) {1, 5}</span></span>
<span class="line"><span style="color:#24292E;">mySet.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#005CC5;">5</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// Set(2) {1, 5} 这里体现了值的唯一性</span></span>
<span class="line"><span style="color:#24292E;">mySet.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;some text&#39;</span><span style="color:#24292E;">)</span></span>
<span class="line"><span style="color:#6A737D;">// Set(3) {1, 5, &quot;some text&quot;} 这里体现了类型的多样性</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> o </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> { a: </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, b: </span><span style="color:#005CC5;">2</span><span style="color:#24292E;"> }</span></span>
<span class="line"><span style="color:#24292E;">mySet.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">(o)</span></span>
<span class="line"><span style="color:#24292E;">mySet.</span><span style="color:#6F42C1;">add</span><span style="color:#24292E;">({ a: </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, b: </span><span style="color:#005CC5;">2</span><span style="color:#24292E;"> })</span></span>
<span class="line"><span style="color:#6A737D;">// Set(5) {1, 5, &quot;some text&quot;, {…}, {…}}</span></span>
<span class="line"><span style="color:#6A737D;">// 这里体现了对象之间引用不同不恒等，即使值相同，Set 也能存储</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li><li><p><strong>类型转换：</strong></p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#6A737D;">// Array 转 Set</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> mySet </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#9ECBFF;">&#39;value1&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;value2&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;value3&#39;</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#6A737D;">// 用...操作符，将 Set 转 Array</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> myArray </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">mySet]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// String 转 Set</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> mySet </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&#39;hello&#39;</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// Set(4) {&quot;h&quot;, &quot;e&quot;, &quot;l&quot;, &quot;o&quot;}</span></span>
<span class="line"><span style="color:#6A737D;">// 注：Set 中 toString 方法是不能将 Set 转换成 String</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#6A737D;">// Array 转 Set</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> mySet </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#032F62;">&#39;value1&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;value2&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;value3&#39;</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#6A737D;">// 用...操作符，将 Set 转 Array</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> myArray </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">mySet]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// String 转 Set</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> mySet </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(</span><span style="color:#032F62;">&#39;hello&#39;</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// Set(4) {&quot;h&quot;, &quot;e&quot;, &quot;l&quot;, &quot;o&quot;}</span></span>
<span class="line"><span style="color:#6A737D;">// 注：Set 中 toString 方法是不能将 Set 转换成 String</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></li><li><p><strong>Set 对象作用：数组去重、并集、交集、差集</strong></p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark has-diff vp-code-dark"><code><span class="line"><span style="color:#6A737D;">// 数组去重</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> mySet </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#E1E4E8;">;[</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">mySet] </span><span style="color:#6A737D;">// [1, 2, 3, 4]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 并集</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> union </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">a, </span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">b]) </span><span style="color:#6A737D;">// {1, 2, 3, 4}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 交集</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> intersect </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">a].</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">((</span><span style="color:#FFAB70;">x</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> b.</span><span style="color:#B392F0;">has</span><span style="color:#E1E4E8;">(x))) </span><span style="color:#6A737D;">// {2, 3}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 差集</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> difference </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">a].</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">((</span><span style="color:#FFAB70;">x</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">!</span><span style="color:#E1E4E8;">b.</span><span style="color:#B392F0;">has</span><span style="color:#E1E4E8;">(x))) </span><span style="color:#6A737D;">// {1}</span></span></code></pre><pre class="shiki github-light has-diff vp-code-light"><code><span class="line"><span style="color:#6A737D;">// 数组去重</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> mySet </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">4</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#24292E;">;[</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">mySet] </span><span style="color:#6A737D;">// [1, 2, 3, 4]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 并集</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> union </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">a, </span><span style="color:#D73A49;">...</span><span style="color:#24292E;">b]) </span><span style="color:#6A737D;">// {1, 2, 3, 4}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 交集</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> intersect </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">a].</span><span style="color:#6F42C1;">filter</span><span style="color:#24292E;">((</span><span style="color:#E36209;">x</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> b.</span><span style="color:#6F42C1;">has</span><span style="color:#24292E;">(x))) </span><span style="color:#6A737D;">// {2, 3}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 差集</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> difference </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">a].</span><span style="color:#6F42C1;">filter</span><span style="color:#24292E;">((</span><span style="color:#E36209;">x</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">!</span><span style="color:#24292E;">b.</span><span style="color:#6F42C1;">has</span><span style="color:#24292E;">(x))) </span><span style="color:#6A737D;">// {1}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></li></ol><h1 id="扩展" tabindex="-1">扩展 <a class="header-anchor" href="#扩展" aria-label="Permalink to &quot;扩展&quot;">​</a></h1><ol><li><p><strong>数组去重</strong></p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> arr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 方法一</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> s </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(arr)</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> newArr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">s]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newArr) </span><span style="color:#6A737D;">// [1, 2]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 方法二</span></span>
<span class="line"><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> newArr1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> Array.</span><span style="color:#B392F0;">from</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(arr))</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newArr1) </span><span style="color:#6A737D;">// [1, 2]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> arr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 方法一</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> s </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(arr)</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> newArr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">s]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newArr) </span><span style="color:#6A737D;">// [1, 2]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 方法二</span></span>
<span class="line"><span style="color:#D73A49;">let</span><span style="color:#24292E;"> newArr1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> Array.</span><span style="color:#6F42C1;">from</span><span style="color:#24292E;">(</span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(arr))</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newArr1) </span><span style="color:#6A737D;">// [1, 2]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li><li><p>数组取并集</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(arr1)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(arr2)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> u </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">([</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">a, </span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">b])</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> newArr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#F97583;">...</span><span style="color:#E1E4E8;">u]</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newArr) </span><span style="color:#6A737D;">// [1, 2, 3, 4]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(arr1)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(arr2)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> u </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">([</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">a, </span><span style="color:#D73A49;">...</span><span style="color:#24292E;">b])</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> newArr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#D73A49;">...</span><span style="color:#24292E;">u]</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newArr) </span><span style="color:#6A737D;">// [1, 2, 3, 4]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></li><li><p>数组取交集</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(arr2)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> newArr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> arr1.</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">((</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> b.</span><span style="color:#B392F0;">has</span><span style="color:#E1E4E8;">(value))</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newArr) </span><span style="color:#6A737D;">// [2, 3]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(arr2)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> newArr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> arr1.</span><span style="color:#6F42C1;">filter</span><span style="color:#24292E;">((</span><span style="color:#E36209;">value</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> b.</span><span style="color:#6F42C1;">has</span><span style="color:#24292E;">(value))</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newArr) </span><span style="color:#6A737D;">// [2, 3]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li><li><p>数组取差集</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr1 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr2 </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> b </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Set</span><span style="color:#E1E4E8;">(arr2)</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> newArr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> arr1.</span><span style="color:#B392F0;">filter</span><span style="color:#E1E4E8;">((</span><span style="color:#FFAB70;">value</span><span style="color:#E1E4E8;">) </span><span style="color:#F97583;">=&gt;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">!</span><span style="color:#E1E4E8;">b.</span><span style="color:#B392F0;">has</span><span style="color:#E1E4E8;">(value))</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(newArr) </span><span style="color:#6A737D;">// [1]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr1 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr2 </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> b </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Set</span><span style="color:#24292E;">(arr2)</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> newArr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> arr1.</span><span style="color:#6F42C1;">filter</span><span style="color:#24292E;">((</span><span style="color:#E36209;">value</span><span style="color:#24292E;">) </span><span style="color:#D73A49;">=&gt;</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">!</span><span style="color:#24292E;">b.</span><span style="color:#6F42C1;">has</span><span style="color:#24292E;">(value))</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(newArr) </span><span style="color:#6A737D;">// [1]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li></ol><h1 id="字符串数组转化为数字数组-互换" tabindex="-1">==字符串数组转化为数字数组（互换）== <a class="header-anchor" href="#字符串数组转化为数字数组-互换" aria-label="Permalink to &quot;==字符串数组转化为数字数组（互换）==&quot;">​</a></h1><ol><li><p>数字数组转化为字符串数组</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> arr </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">4</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">5</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">6</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">7</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">8</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">9</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">arr.</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(String) </span><span style="color:#6A737D;">//结果： [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> arr </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">4</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">5</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">6</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">7</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">8</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">9</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#24292E;">arr.</span><span style="color:#6F42C1;">map</span><span style="color:#24292E;">(String) </span><span style="color:#6A737D;">//结果： [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li><li><p>字符串数组转化为数字数组</p><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;1&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;2&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;3&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;4&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;5&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;6&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;7&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;8&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;9&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#E1E4E8;">a.</span><span style="color:#B392F0;">map</span><span style="color:#E1E4E8;">(Number) </span><span style="color:#6A737D;">//结果：[1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;1&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;2&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;3&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;4&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;5&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;6&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;7&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;8&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;9&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#24292E;">a.</span><span style="color:#6F42C1;">map</span><span style="color:#24292E;">(Number) </span><span style="color:#6A737D;">//结果：[1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ol><h1 id="其他-了解即可" tabindex="-1">其他（了解即可） <a class="header-anchor" href="#其他-了解即可" aria-label="Permalink to &quot;其他（了解即可）&quot;">​</a></h1><h2 id="_1-数组与对象的对比" tabindex="-1">1.数组与对象的对比 <a class="header-anchor" href="#_1-数组与对象的对比" aria-label="Permalink to &quot;1.数组与对象的对比&quot;">​</a></h2><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> array </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">, </span><span style="color:#79B8FF;">3</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> item </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> array[</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">] </span><span style="color:#6A737D;">// 访问数组元素</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> person </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> { name: </span><span style="color:#9ECBFF;">&#39;Mrshang110&#39;</span><span style="color:#E1E4E8;">, age: </span><span style="color:#79B8FF;">24</span><span style="color:#E1E4E8;"> }</span></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> name </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> person[</span><span style="color:#9ECBFF;">&#39;name&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#6A737D;">// 访问对象元素</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> array </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">2</span><span style="color:#24292E;">, </span><span style="color:#005CC5;">3</span><span style="color:#24292E;">]</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> item </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> array[</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">] </span><span style="color:#6A737D;">// 访问数组元素</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> person </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> { name: </span><span style="color:#032F62;">&#39;Mrshang110&#39;</span><span style="color:#24292E;">, age: </span><span style="color:#005CC5;">24</span><span style="color:#24292E;"> }</span></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> name </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> person[</span><span style="color:#032F62;">&#39;name&#39;</span><span style="color:#24292E;">] </span><span style="color:#6A737D;">// 访问对象元素</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="_2-数组的多种遍历方式" tabindex="-1">2.数组的多种遍历方式 <a class="header-anchor" href="#_2-数组的多种遍历方式" aria-label="Permalink to &quot;2.数组的多种遍历方式&quot;">​</a></h2><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">const</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">ary</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> [</span><span style="color:#9ECBFF;">&#39;a&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;b&#39;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&#39;c&#39;</span><span style="color:#E1E4E8;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 最基本的方式, 只能遍历下标有序递增的数组</span></span>
<span class="line"><span style="color:#F97583;">for</span><span style="color:#E1E4E8;"> (</span><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> i </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">0</span><span style="color:#E1E4E8;">; i </span><span style="color:#F97583;">&lt;</span><span style="color:#E1E4E8;"> ary.</span><span style="color:#79B8FF;">length</span><span style="color:#E1E4E8;">; i</span><span style="color:#F97583;">++</span><span style="color:#E1E4E8;">) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(i, ary[i]) </span><span style="color:#6A737D;">// 0 &#39;a&#39;  1 &#39;b&#39;  2 &#39;c&#39;</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// in:每遍历一次数组指针向后移动一位, 并得到当前数组元素值的下标, 可以通过 ary[key] 来访问数组元素值</span></span>
<span class="line"><span style="color:#F97583;">for</span><span style="color:#E1E4E8;"> (</span><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> key </span><span style="color:#F97583;">in</span><span style="color:#E1E4E8;"> ary) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(key, ary[key]) </span><span style="color:#6A737D;">// 0 a  1 b  2 c</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// of:每遍历一次数组指针向后移动一位, 并得到当前数组元素的值, 处理二维数组时极为方便</span></span>
<span class="line"><span style="color:#F97583;">for</span><span style="color:#E1E4E8;"> (</span><span style="color:#F97583;">let</span><span style="color:#E1E4E8;"> value </span><span style="color:#F97583;">of</span><span style="color:#E1E4E8;"> ary) {</span></span>
<span class="line"><span style="color:#E1E4E8;">  console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(value) </span><span style="color:#6A737D;">// a b c</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">const</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">ary</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> [</span><span style="color:#032F62;">&#39;a&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;b&#39;</span><span style="color:#24292E;">, </span><span style="color:#032F62;">&#39;c&#39;</span><span style="color:#24292E;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// 最基本的方式, 只能遍历下标有序递增的数组</span></span>
<span class="line"><span style="color:#D73A49;">for</span><span style="color:#24292E;"> (</span><span style="color:#D73A49;">let</span><span style="color:#24292E;"> i </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">0</span><span style="color:#24292E;">; i </span><span style="color:#D73A49;">&lt;</span><span style="color:#24292E;"> ary.</span><span style="color:#005CC5;">length</span><span style="color:#24292E;">; i</span><span style="color:#D73A49;">++</span><span style="color:#24292E;">) {</span></span>
<span class="line"><span style="color:#24292E;">  console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(i, ary[i]) </span><span style="color:#6A737D;">// 0 &#39;a&#39;  1 &#39;b&#39;  2 &#39;c&#39;</span></span>
<span class="line"><span style="color:#24292E;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// in:每遍历一次数组指针向后移动一位, 并得到当前数组元素值的下标, 可以通过 ary[key] 来访问数组元素值</span></span>
<span class="line"><span style="color:#D73A49;">for</span><span style="color:#24292E;"> (</span><span style="color:#D73A49;">let</span><span style="color:#24292E;"> key </span><span style="color:#D73A49;">in</span><span style="color:#24292E;"> ary) {</span></span>
<span class="line"><span style="color:#24292E;">  console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(key, ary[key]) </span><span style="color:#6A737D;">// 0 a  1 b  2 c</span></span>
<span class="line"><span style="color:#24292E;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#6A737D;">// of:每遍历一次数组指针向后移动一位, 并得到当前数组元素的值, 处理二维数组时极为方便</span></span>
<span class="line"><span style="color:#D73A49;">for</span><span style="color:#24292E;"> (</span><span style="color:#D73A49;">let</span><span style="color:#24292E;"> value </span><span style="color:#D73A49;">of</span><span style="color:#24292E;"> ary) {</span></span>
<span class="line"><span style="color:#24292E;">  console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(value) </span><span style="color:#6A737D;">// a b c</span></span>
<span class="line"><span style="color:#24292E;">}</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="" alt="image-20220328225130536"></p><h2 id="_3-数组对象赋值与数组元素赋值" tabindex="-1">3.数组对象赋值与数组元素赋值 <a class="header-anchor" href="#_3-数组对象赋值与数组元素赋值" aria-label="Permalink to &quot;3.数组对象赋值与数组元素赋值&quot;">​</a></h2><div class="language-js vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">js</span><pre class="shiki github-dark vp-code-dark"><code><span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Array</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#9ECBFF;">&#39;a1&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">1</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#9ECBFF;">&#39;a2&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">2</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a.</span><span style="color:#79B8FF;">length</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 输出0</span></span>
<span class="line"><span style="color:#6A737D;">//实例中由于你没有给数组的元素赋值，所以 a 数组的长度也没有被创建出来，此时 a.length 输出结果为 0。（即只给数组对象赋值没给数组元素赋值。）</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Array</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#9ECBFF;">&#39;a1&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">1</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#9ECBFF;">&#39;a2&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">2</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#79B8FF;">2</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">3</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a.</span><span style="color:#79B8FF;">length</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 输出3</span></span>
<span class="line"><span style="color:#6A737D;">//在最后一行代码中数组元素 a[2] 赋值了，此时 JavaScript 会给数组 a 创建一个长度为 3 的空间，并在第三个元素中赋以3，但是元素1和元素2都没有赋值，所以此时a[0]和a[1]是没有值的。</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F97583;">var</span><span style="color:#E1E4E8;"> a </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">Array</span><span style="color:#E1E4E8;">()</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#9ECBFF;">&#39;a1&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">1</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#9ECBFF;">&#39;100&#39;</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#79B8FF;">3</span></span>
<span class="line"><span style="color:#E1E4E8;">a[</span><span style="color:#F97583;">-</span><span style="color:#79B8FF;">1</span><span style="color:#E1E4E8;">] </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&#39;yes&#39;</span></span>
<span class="line"><span style="color:#E1E4E8;">console.</span><span style="color:#B392F0;">log</span><span style="color:#E1E4E8;">(a.</span><span style="color:#79B8FF;">length</span><span style="color:#E1E4E8;">) </span><span style="color:#6A737D;">// 输出 101</span></span>
<span class="line"><span style="color:#6A737D;">//Javascript 数组下标值的范围为 0 ~ 2的32次方。</span></span>
<span class="line"><span style="color:#6A737D;">//如果在合法范围内，则无论该下标值是数字还是数字字符串，都一律会被转化为数字使用，即 a[&quot;100&quot;] = 0 和 a[100] = 0 执行的是相同的操作。</span></span>
<span class="line"><span style="color:#6A737D;">//如果不在此范围内，Javascript 会将它转换为一个字符串。a[-1] = &quot;yes&quot; 其实就相当于给数组对象添加了一个名为 -1 的属性，属性值为 yes。</span></span></code></pre><pre class="shiki github-light vp-code-light"><code><span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Array</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#032F62;">&#39;a1&#39;</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">1</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#032F62;">&#39;a2&#39;</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">2</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a.</span><span style="color:#005CC5;">length</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 输出0</span></span>
<span class="line"><span style="color:#6A737D;">//实例中由于你没有给数组的元素赋值，所以 a 数组的长度也没有被创建出来，此时 a.length 输出结果为 0。（即只给数组对象赋值没给数组元素赋值。）</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Array</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#032F62;">&#39;a1&#39;</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">1</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#032F62;">&#39;a2&#39;</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">2</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#005CC5;">2</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">3</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a.</span><span style="color:#005CC5;">length</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 输出3</span></span>
<span class="line"><span style="color:#6A737D;">//在最后一行代码中数组元素 a[2] 赋值了，此时 JavaScript 会给数组 a 创建一个长度为 3 的空间，并在第三个元素中赋以3，但是元素1和元素2都没有赋值，所以此时a[0]和a[1]是没有值的。</span></span>
<span class="line"></span>
<span class="line"><span style="color:#D73A49;">var</span><span style="color:#24292E;"> a </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#D73A49;">new</span><span style="color:#24292E;"> </span><span style="color:#6F42C1;">Array</span><span style="color:#24292E;">()</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#032F62;">&#39;a1&#39;</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">1</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#032F62;">&#39;100&#39;</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#005CC5;">3</span></span>
<span class="line"><span style="color:#24292E;">a[</span><span style="color:#D73A49;">-</span><span style="color:#005CC5;">1</span><span style="color:#24292E;">] </span><span style="color:#D73A49;">=</span><span style="color:#24292E;"> </span><span style="color:#032F62;">&#39;yes&#39;</span></span>
<span class="line"><span style="color:#24292E;">console.</span><span style="color:#6F42C1;">log</span><span style="color:#24292E;">(a.</span><span style="color:#005CC5;">length</span><span style="color:#24292E;">) </span><span style="color:#6A737D;">// 输出 101</span></span>
<span class="line"><span style="color:#6A737D;">//Javascript 数组下标值的范围为 0 ~ 2的32次方。</span></span>
<span class="line"><span style="color:#6A737D;">//如果在合法范围内，则无论该下标值是数字还是数字字符串，都一律会被转化为数字使用，即 a[&quot;100&quot;] = 0 和 a[100] = 0 执行的是相同的操作。</span></span>
<span class="line"><span style="color:#6A737D;">//如果不在此范围内，Javascript 会将它转换为一个字符串。a[-1] = &quot;yes&quot; 其实就相当于给数组对象添加了一个名为 -1 的属性，属性值为 yes。</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div></div></div></main><footer class="VPDocFooter" data-v-42620db0 data-v-c7f3a203><!--[--><!--]--><!----><!----></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><footer class="VPFooter" data-v-9318537a data-v-6e61ade4><div class="container" data-v-6e61ade4><p class="message" data-v-6e61ade4>根据 MIT 许可证发布</p><p class="copyright" data-v-6e61ade4>Copyright (c) 2024 xiaobinxiaoxiong</p></div></footer><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"index.md\":\"e0dee572\",\"nav_index.md\":\"a9a25e99\",\"daily-notes_字符串.md\":\"2d5c7bf1\",\"daily-notes_数组.md\":\"ca3d7d97\",\"note_web_vue3_vue3快速上手.md\":\"03582f9a\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"zh-CN\",\"dir\":\"ltr\",\"title\":\"小盐菌\",\"description\":\"包含前端常用知识、源码阅读笔记、各种奇淫技巧、日常提效工具等\",\"base\":\"/vitepress_note_public/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"i18nRouting\":false,\"logo\":\"/logo.png\",\"nav\":[{\"text\":\"首页\",\"link\":\"/\"},{\"text\":\"待开发\",\"link\":\"/nav/\"},{\"text\":\"待开发\",\"link\":\"https://notes.fe-mm.com\"},{\"text\":\"待开发\",\"link\":\"https://netease-music.fe-mm.com\"},{\"text\":\"待开发\",\"link\":\"https://github.com/maomao1996/tampermonkey-scripts\"}],\"sidebar\":{},\"outline\":{\"level\":\"deep\",\"label\":\"本页目录\"},\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/maomao1996/vitepress-nav-template\"},{\"icon\":{\"svg\":\"<svg t=\\\"1703841371820\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"7872\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M683.058 364.695c11 0 22 1.016 32.943 1.976C686.564 230.064 538.896 128 370.681 128c-188.104 0.66-342.237 127.793-342.237 289.226 0 93.068 51.379 169.827 136.725 229.256L130.72 748.43l119.796-59.368c42.918 8.395 77.37 16.79 119.742 16.79 11 0 21.46-0.48 31.914-1.442a259.168 259.168 0 0 1-10.455-71.358c0.485-148.002 128.744-268.297 291.403-268.297l-0.06-0.06z m-184.113-91.992c25.99 0 42.913 16.79 42.913 42.575 0 25.188-16.923 42.579-42.913 42.579-25.45 0-51.38-16.85-51.38-42.58 0-25.784 25.93-42.574 51.38-42.574z m-239.544 85.154c-25.384 0-51.374-16.85-51.374-42.58 0-25.784 25.99-42.574 51.374-42.574 25.45 0 42.918 16.79 42.918 42.575 0 25.188-16.924 42.579-42.918 42.579z m736.155 271.655c0-135.647-136.725-246.527-290.983-246.527-162.655 0-290.918 110.88-290.918 246.527 0 136.128 128.263 246.587 290.918 246.587 33.972 0 68.423-8.395 102.818-16.85l93.809 50.973-25.93-84.677c68.907-51.93 120.286-119.815 120.286-196.033z m-385.275-42.58c-16.923 0-34.452-16.79-34.452-34.179 0-16.79 17.529-34.18 34.452-34.18 25.99 0 42.918 16.85 42.918 34.18 0 17.39-16.928 34.18-42.918 34.18z m188.165 0c-16.984 0-33.972-16.79-33.972-34.179 0-16.79 16.927-34.18 33.972-34.18 25.93 0 42.913 16.85 42.913 34.18 0 17.39-16.983 34.18-42.913 34.18z\\\" fill=\\\"#09BB07\\\" p-id=\\\"7873\\\"></path></svg>\"},\"link\":\"https://weixin.qq.com/\"},{\"icon\":{\"svg\":\"<svg t=\\\"1703841714246\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"9766\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z\\\" fill=\\\"#C71D23\\\" p-id=\\\"9767\\\"></path></svg>\"},\"link\":\"https://gitee.com/\"},{\"icon\":{\"svg\":\"<svg t=\\\"1703842691600\\\" class=\\\"icon\\\" viewBox=\\\"0 0 1024 1024\\\" version=\\\"1.1\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" p-id=\\\"10969\\\" width=\\\"200\\\" height=\\\"200\\\"><path d=\\\"M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z\\\" fill=\\\"#FF6B9B\\\" p-id=\\\"10970\\\"></path><path d=\\\"M336.896 115.584l117.568 115.968h115.008l117.632-115.904a36.672 36.672 0 0 1 51.328 0 35.456 35.456 0 0 1 0 50.56L672 231.552h75.648c33.664 0 65.92 13.184 89.792 36.672 23.808 23.488 37.12 55.296 37.12 88.512v286.08c0 33.216-13.312 65.024-37.12 88.512a127.872 127.872 0 0 1-89.792 36.672H276.288c-33.664 0-65.92-13.184-89.792-36.672a124.288 124.288 0 0 1-37.12-88.512v-286.08c0-33.216 13.312-65.024 37.12-88.512a127.872 127.872 0 0 1 89.792-36.672h75.584L285.568 166.144a35.456 35.456 0 0 1-9.344-34.56 36.608 36.608 0 0 1 60.736-16z m410.88 187.52H276.224a54.144 54.144 0 0 0-54.144 48.512l-0.256 5.12v286.08c0 27.648 21.248 50.752 49.152 53.44l5.248 0.192h471.424a54.144 54.144 0 0 0 54.144-48.448l0.256-5.12v-286.08a54.016 54.016 0 0 0-54.4-53.76z m-380.8 107.264c19.968 0 36.224 16 36.224 35.776v71.552a36.032 36.032 0 0 1-36.288 35.712 36.032 36.032 0 0 1-36.224-35.712V446.08c0-19.776 16.192-35.776 36.224-35.776z m290.112 0c20.032 0 36.224 16 36.224 35.776v71.552a36.032 36.032 0 0 1-36.224 35.712 36.032 36.032 0 0 1-36.288-35.712V446.08c0-19.776 16.256-35.776 36.288-35.776z\\\" fill=\\\"#FF7BA6\\\" p-id=\\\"10971\\\"></path><path d=\\\"M741.632 390.72c2.496 0.448 22.208-3.968 23.04-2.56 1.28 1.92 10.048 65.152 7.68 65.6-2.24 0.384-18.56 4.16-18.56 4.16-1.28-8.576-11.968-61.76-12.16-67.2m31.36-6.08l8.64 68.032c4.352-0.192 22.144-1.472 23.872-1.664-2.56-25.728-6.912-66.368-6.912-66.368a65.536 65.536 0 0 0-25.6 0m-16.896 94.464s38.72-9.856 52.736-5.056c7.104 25.728 20.096 170.24 21.376 176.064-8.832 1.088-37.76 3.584-40 4.224-1.92-10.88-34.112-168.32-34.112-175.232m143.36-86.72c2.368 0.64 22.848-2.112 23.04-0.64 0.448 5.056 2.56 65.792 0.256 65.92l-18.688 1.728c-0.384-8.32-4.992-61.376-4.544-67.008m31.616-1.664l1.664 67.2c4.352 0 21.952 1.024 23.872 0.832-0.448-25.728 0-66.368 0-66.368a115.712 115.712 0 0 0-25.6-1.664m-27.008 90.24s39.616-5.248 52.992 1.024c2.944 30.144 2.56 170.688 2.944 176.512-8.96 0-37.696 0.448-40 0.832-0.384-10.88-16.512-171.456-15.936-178.368M812.16 308.224c22.848 116.416 40.192 315.328 40.832 328.064 0 0 18.048 0.448 38.336 1.728-11.968-125.44-26.624-325.76-26.368-332.288-5.056-5.696-52.8 2.496-52.8 2.496m-50.24 275.328c-4.416-32.64-121.216-69.76-186.944-57.792 0 0-8.192-72-11.328-141.76a1447.04 1447.04 0 0 1 0.192-127.68c-4.352-3.2-51.712 19.456-77.248 28.864 0 0 30.592 129.408 52.736 397.824 0 0 35.392 3.776 96.128-7.936s132.544-48.384 126.464-91.52m-142.784 58.88l-10.24-73.984c2.496-1.28 65.536 22.208 72.192 26.432-1.024 4.608-61.952 47.488-61.952 47.488m-364.16-251.648c2.56 0.448 22.272-3.968 23.04-2.56 1.28 1.92 10.112 65.152 7.808 65.6-2.304 0.384-18.688 4.16-18.688 4.16-1.28-8.576-11.904-61.76-12.16-67.2m31.424-6.08l8.64 68.032c4.352-0.192 22.144-1.472 23.872-1.664-2.752-25.728-6.912-66.368-6.912-66.368a65.536 65.536 0 0 0-25.6 0M269.44 479.104s38.784-9.856 52.8-5.056c7.104 25.728 20.096 170.24 21.376 176.064-8.832 1.088-37.76 3.584-40 4.224-1.92-10.88-34.176-168.32-34.176-175.232M412.864 392.32c2.304 0.64 22.848-2.112 23.04-0.64 0.448 5.056 2.56 65.792 0.256 65.92l-18.688 1.728c-0.384-8.32-4.8-61.376-4.608-67.008m31.616-1.664l1.728 67.2c4.352 0 21.952 1.024 23.872 0.832-0.448-25.728 0-66.368 0-66.368a129.92 129.92 0 0 0-25.6-1.664m-27.008 90.24s39.616-5.248 52.992 1.024c2.944 30.144 2.56 170.688 2.944 176.512-8.96 0-37.696 0.448-40 0.832-0.192-10.88-16.512-171.456-15.936-178.368M325.568 308.224c22.848 116.416 40.192 315.328 40.832 328.064 0 0 18.048 0.448 38.336 1.728-11.968-125.44-26.624-325.76-26.368-332.544-5.056-5.44-52.8 2.752-52.8 2.752m-50.24 275.328c-4.416-32.64-121.216-69.76-186.944-57.792 0 0-8.192-72-11.328-141.76a1447.04 1447.04 0 0 1 0.192-127.68C72.832 253.376 25.6 275.776 0 285.184c0 0 30.592 129.408 52.736 397.824 0 0 35.392 3.776 96.128-7.936s132.48-48.384 126.464-91.52m-142.784 58.88l-10.24-73.984c2.496-1.28 65.536 22.208 72.192 26.432-1.024 4.608-61.952 47.488-61.952 47.488\\\" fill=\\\"#FFFFFF\\\" p-id=\\\"10972\\\"></path></svg>\"},\"link\":\"https://www.bilibili.com/\"}],\"footer\":{\"message\":\"根据 MIT 许可证发布\",\"copyright\":\"Copyright (c) 2024 xiaobinxiaoxiong\"},\"darkModeSwitchLabel\":\"外观\",\"returnToTopLabel\":\"返回顶部\",\"lastUpdatedText\":\"上次更新\",\"docFooter\":{\"prev\":\"上一篇\",\"next\":\"下一篇\"}},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":true}");</script>
    
  </body>
</html>